| 1 | package net.mandaria.tippytipper.preferences; |
| 2 | |
| 3 | import net.mandaria.tippytipper.R; |
| 4 | import net.mandaria.tippytipper.widgets.*; |
| 5 | import android.content.Context; |
| 6 | import android.util.AttributeSet; |
| 7 | import android.view.Gravity; |
| 8 | import android.view.View; |
| 9 | import android.preference.DialogPreference; |
| 10 | import android.widget.TableLayout; |
| 11 | import android.widget.TableRow; |
| 12 | import android.widget.TextView; |
| 13 | import android.content.res.*; |
| 14 | |
| 15 | public class NumberPickerPreference extends DialogPreference |
| 16 | { |
| 17 | private static final String androidns = "http://schemas.android.com/apk/res/android"; |
| 18 | |
| 19 | private NumberPicker mPickInteger; |
| 20 | private TextView mSplashText; |
| 21 | private Context mContext; |
| 22 | |
| 23 | private String mDialogMessage, mSuffix; |
| 24 | private int mDefault, mMin, mMax, mValue = 0; |
| 25 | |
| 26 | public NumberPickerPreference(Context context, AttributeSet attrs) |
| 27 | { |
| 28 | super(context, attrs); |
| 29 | mContext = context; |
| 30 | |
| 31 | mDialogMessage = attrs.getAttributeValue(androidns, "dialogMessage"); |
| 32 | mSuffix = attrs.getAttributeValue(androidns, "text"); |
| 33 | mDefault = attrs.getAttributeIntValue(androidns, "defaultValue", 0); |
| 34 | mMax = attrs.getAttributeIntValue(androidns,"max", 100); |
| 35 | |
| 36 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SeekBarPreference); |
| 37 | mMin = a.getInt(R.styleable.SeekBarPreference_min, 0); |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | protected View onCreateDialogView() |
| 42 | { |
| 43 | |
| 44 | TableLayout layout = new TableLayout(mContext); |
| 45 | layout.setPadding(6, 6, 6, 6); |
| 46 | |
| 47 | mSplashText = new TextView(mContext); |
| 48 | if (mDialogMessage != null) |
| 49 | mSplashText.setText(mDialogMessage); |
| 50 | |
| 51 | TableRow row_header = new TableRow(mContext); |
| 52 | row_header.addView(mSplashText); |
| 53 | |
| 54 | mPickInteger = new NumberPicker(mContext); |
| 55 | mPickInteger.setRange(mMin, mMax); |
| 56 | |
| 57 | TextView suffix = new TextView(mContext); |
| 58 | suffix.setText(mSuffix); |
| 59 | suffix.setTextSize(32); |
| 60 | |
| 61 | TableRow row_one = new TableRow(mContext); |
| 62 | row_one.setGravity(Gravity.CENTER); |
| 63 | row_one.addView(mPickInteger); |
| 64 | row_one.addView(suffix); |
| 65 | |
| 66 | layout.addView(row_header); |
| 67 | |
| 68 | TableLayout table_main = new TableLayout(mContext); |
| 69 | table_main.addView(row_one); |
| 70 | |
| 71 | TableRow row_main = new TableRow(mContext); |
| 72 | row_main.setGravity(Gravity.CENTER_HORIZONTAL); |
| 73 | row_main.addView(table_main); |
| 74 | |
| 75 | layout.addView(row_main); |
| 76 | |
| 77 | if (shouldPersist()) |
| 78 | mValue = getPersistedInt(mDefault); |
| 79 | |
| 80 | bindData(); |
| 81 | |
| 82 | return layout; |
| 83 | } |
| 84 | |
| 85 | private void bindData() |
| 86 | { |
| 87 | try |
| 88 | { |
| 89 | mPickInteger.setCurrent(mValue); |
| 90 | } |
| 91 | catch (Exception ex) |
| 92 | { |
| 93 | |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | protected void onBindDialogView(View v) |
| 99 | { |
| 100 | super.onBindDialogView(v); |
| 101 | bindData(); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | protected void onSetInitialValue(boolean restore, Object defaultValue) |
| 106 | { |
| 107 | super.onSetInitialValue(restore, defaultValue); |
| 108 | if (restore) |
| 109 | { |
| 110 | try |
| 111 | { |
| 112 | mValue = shouldPersist() ? getPersistedInt(mDefault) : 0; |
| 113 | } |
| 114 | catch (Exception ex) |
| 115 | { |
| 116 | mValue = mDefault; |
| 117 | } |
| 118 | } |
| 119 | else |
| 120 | mValue = (Integer) defaultValue; |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | protected void onDialogClosed(boolean positiveResult) |
| 125 | { |
| 126 | if (positiveResult == true) |
| 127 | { |
| 128 | super.onDialogClosed(positiveResult); |
| 129 | // HACK: "click" both picker inputs to validate inputs before closing the dialog |
| 130 | // this is to fix a problem of closing the dialog not causing the onFocusChange of the picker |
| 131 | // to be called |
| 132 | mPickInteger.onClick(null); |
| 133 | mValue = mPickInteger.getCurrent(); |
| 134 | if (shouldPersist()) |
| 135 | persistInt(mValue); |
| 136 | } |
| 137 | } |
| 138 | } |